#!/bin/bash

export PATH=/VI/mysql/bin:$PATH

currentpath=$(cd `dirname $0`; pwd)
videoinsight_Setup=${currentpath}/setup.sh

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
fi

echo "=============================init database...============================="

echo "create database..."
mysql -hlocalhost -uroot -pVideoInsight_123 -e "CREATE DATABASE videoinsight DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"
mysql -hlocalhost -uroot -pVideoInsight_123 -e "CREATE DATABASE videoinsightcollecter DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"

#mysql -hlocalhost -uroot -pVideoInsight_123 -e "use mysql; GRANT all privileges ON *.* TO 'videoinsight'@'%';"
#mysql -hlocalhost -uroot -pVideoInsight_123 -e "use mysql; GRANT all privileges ON *.* TO 'videoinsightcollecter'@'%';"


echo "create tables..."

for i in `ls ${currentpath}/sql/videoinsightCollecter`
do
     mysql -uroot -pVideoInsight_123 << EOF
     use videoinsightcollecter;
     source ${currentpath}/sql/videoinsightCollecter/$i;
EOF
done

for i in `ls ${currentpath}/sql/videoinsight`
do
     mysql -uroot -pVideoInsight_123 << EOF
     use videoinsight;
     source ${currentpath}/sql/videoinsight/$i;
EOF
done

rm -rf ${currentpath}/sql
rm -rf ${currentpath}/database

echo "restart mysql"
mv ${currentpath}/dog /VI/mysql/bin/
chown mysql:mysql /VI/mysql/bin/dog
sh /VI/mysql/bin/dog restart

echo "INIT MySQL SUCCESS!"
